package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.SecondHandCar;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *   玩乐住, 美食街,石子街 公用的请求数据了类-列表 异步请求
 */
public class GetSecondHandCarSearch extends BaseCommand {

    private int catid; //当前分类编号
    private int page =  DataCenter.LISTVIEW_DEFAULT_PAGE; //当前页码
    private int  order = 0;//排序方式
    private int price;
    private String brand;
    private int caryear;
    private int mileage;
    private String TAG = "GetSecondHandHouseLIst";

    public GetSecondHandCarSearch(int price, String brand, int caryear, int mileage, int page) {
        this.price = price;
        this.brand = brand;
        this.caryear = caryear;
        this.mileage = mileage;
        this.page = page;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_SECOND_HAND_CAR_SEARCH;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("price", price  + "");
        paramters.put("brand",brand);
        paramters.put("caryear",caryear+"");
        paramters.put("mileage",mileage+"");
        paramters.put("page", page + "");
        paramters.put("order",order + "");
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetSecondHandHouseLIst.yes()");

        try {
            Log.d(TAG, result.object.toString());

            JSONObject jsonObject = new JSONObject(result.object.toString());
            JSONArray jsonArray = jsonObject.optJSONArray("data");
            if (page == DataCenter.PAGE) {
                for (int i = DataCenter.secondHandCar.size()-1; i >= (DataCenter.car_position/10+1)*10-10; i--) {
                    DataCenter.secondHandCar.remove(i);
                }
            }
            ArrayList<SecondHandCar> targetList = DataCenter.secondHandCar;

            if (page == DataCenter.LISTVIEW_DEFAULT_PAGE) {
                targetList.clear();
            }

            for(int i = 0; i < jsonArray.length(); ++i){ // i从1开始，屏蔽提示信息
                JSONObject o = jsonArray.getJSONObject(i);
                SecondHandCar bean = JsonUtil.jsonToObject(o.toString(), SecondHandCar.class);
                targetList.add(bean);
            }
            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_SECOND_HAND_CAR_LIST_SUCCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetSecondHandHouseLIst.no()");

//        DataCenter.secondHandHouse.clear();
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_SECOND_HAND_CAR_FAILED);

    }
}
